<?php

defined( '_JEXEC' ) or die( 'Restricted access' );
jimport('joomla.application.component.controller');
jimport('joomla.application.component.model');

class ScorecardController extends JController
{

	function display() {
		$user =& JFactory::getUser();
		
		// User has not logged in yet
		if ($user->get('guest')) {
			$return = $_SERVER['HTTP_REFERER'] ? base64_encode($_SERVER['HTTP_REFERER']) : base64_encode('index.php?option=com_scorecard&view=score');
			$url = 'index.php?option=com_user&view=login&return=' . $return;
			$this->setRedirect($url, 'You must login first');
			return false;
		}
		$this->addModelPath(JPATH_COMPONENT . DS . 'models');
		parent::display();
	}

	function addCourse() {
		$model =& JModel::getInstance('course', 'ScorecardModel');
		if ($model && $model->saveCourse()) {
			$this->setRedirect('index.php?option=com_scorecard&view=score&Itemid=' . JRequest::getVar('Itemid'), JText::_('Golf course saved successfully.'));
		} else {
			$this->setRedirect('index.php?option=com_scorecard&view=score&Itemid=' . JRequest::getVar('Itemid'), JText::_('Failed to save golf course.'), 'error');
		}
	}

	function scoreSearch() {
		$this->display();
	}

	function scorecardSearch() {
		$this->display();
	}

	function saveScorecard() {
		$model =& JModel::getInstance('scorecard', 'ScorecardModel');
		if ($model && $model->saveScorecard()) {
			$this->setRedirect('index.php?option=com_scorecard&view=score&Itemid=' . JRequest::getVar('Itemid'), 'Score saved sucessfully.');
		} else {
			$this->setRedirect($_SERVER['HTTP_REFERER'], 'Failed to save score.', 'error');
		}
	}

	function updateForm() {
		$this->display();
	}

	function resetForm() {
		$this->setRedirect($_SERVER['HTTP_REFERER']);
	}
}
?>
